<?php
/**
 * vim: et ts=4 sts=4 sw=4 fdm=marker
 *
 * 测试controller 
 *
 * @author	wang Penghai <china_php@hotmail.com>
 * @copyright 2008 5e6d Corporation.
 * @version   $Id: config.php 29 2009-03-10 14:30:18Z wph $
 */
if(!isset($_SESSION)) 
{
	 session_start();
}

class login_action extends pf_controller{
	protected $menu_obj=null;
	public function __construct()
	{
		parent::__construct();
		$this->admin_obj = pf_load::init('admin_model_admin');
		$this->admin_group_obj = pf_load::init('admin_model_group');
		$this->showmessage = pf_load::init('admin_class_showmessage');
	}

	public function index()
	{
		$this->view('admin_admin_view_login');
	}

	//退出登陆
	public function login_out()
	{
		unset($_SESSION['admin'], $_COOKIE['userinfo']);
		$this->showmessage->html('退出成功!', $this->set_url('admin', 'login'));
		exit;
	}

	//登录
	public function check_login()
	{
		$username = pf_request::post('username');
		$password = pf_request::post('password');
		if (empty($username) || empty($password))
		{
			$this->assign('msg', '用户名或密码不能为空！');
			$this->view('admin_admin_view_login');
			exit;
		}

		$userinfo = $this->admin_obj->get_info_by_username($username);
		if (false === $userinfo || empty($userinfo))
		{
			$this->assign('msg', '用户名或密码错误！');
			$this->view('admin_admin_view_login');
			exit;
		}
		$password = md5($password);
		foreach ($userinfo as $val)
		{
			if (strcmp($val['password'], $password) === 0)
			{
				$gid = $val['group_id'];
				$group_info = $this->admin_group_obj->get_group_by_gid($gid);
				$prov_list = $group_info['access_list'];
				$_SESSION['admin'] = $val['id'] .'|' . $prov_list . '|' . md5($val['username'] . '|' . $group_info['name']);
				setcookie('userinfo',  $val['username'] . '|' . $group_info['name']);
				header('location: ' . $this->set_url());
				break;
			}
		}

		$this->assign('msg', '用户名或密码错误！');
		$this->view('admin_admin_view_login');
		exit;
	}
}